/*******************************************************************************
 * Copyright 2011 Hani Naguib
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.gvmax.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Class used to access GVMax.
 * 
 * @author Hani Naguib
 */
public class GVMaxAPI {
	public static final String GVMAX_BASE_API_URL = "https://www.gvmax.com/api"; 
	/** User's API pin */
	private String pin;
	
	/**
	 * Constructor that uses the user's pin for access. 
	 * @param pin The user's pin.
	 */
	public GVMaxAPI(String pin) throws IllegalArgumentException {
		MiscUtil.validateNotEmpty("pin", pin);
		this.pin = pin; 
	}
	
	public UserInfo getUserInfo() throws IOException {
		String jsonStr = MiscUtil.doGet(GVMAX_BASE_API_URL+"/user.json?pin="+pin);
		try {
			JSONObject jobj = new JSONObject(jsonStr);
			JSONObject user = jobj.getJSONObject("user");
			JSONObject stats = jobj.getJSONObject("stats");
			UserInfo info = new UserInfo();
			info.setEmail(user.getString("email"));
			info.setGvPassword(user.getBoolean("gvPassword"));
			info.setMonitorSMS(user.getBoolean("monitorSMS"));
			info.setMonitorVM(user.getBoolean("monitorVM"));
			info.setMonitorMC(user.getBoolean("monitorMC"));
			info.setSendGTalk(user.getBoolean("sendGTalk"));
			info.setgTalkEmail(user.getString("gTalkEmail"));
			info.setgTalkGroup(user.getString("gTalkGroup"));
			info.setSendPost(user.getBoolean("sendPost"));
			info.setPostURLs(user.getString("postURLs"));
			// TODO: Other stuff
			info.setSmsInCount(stats.getInt("smsInCount"));
			info.setVmInCount(stats.getInt("vmInCount"));
			info.setMcInCount(stats.getInt("mcInCount"));
			info.setSmsOutCount(stats.getInt("smsOutCount"));
			info.setEmailInCount(stats.getInt("emailInCount"));
			info.setgTalkCount(stats.getInt("gTalkCount"));
			info.setApiCount(stats.getInt("apiCount"));
			info.setErrorCount(stats.getInt("errorCount"));
			info.setInvalidEmailCount(stats.getInt("invalidEmailCount"));
			info.setFallbackCount(stats.getInt("fallbackCount"));
			return info;
		} catch (JSONException e) {
			throw new IOException(e.getMessage());
		}
	}
	
	public void setMonitors(boolean SMS, boolean VM, boolean MC) throws IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("pin", pin);
		params.put("monitorSMS", Boolean.toString(SMS));
		params.put("monitorVM", Boolean.toString(VM));
		params.put("monitorMC", Boolean.toString(MC));
		MiscUtil.doPost(GVMAX_BASE_API_URL+"/monitors.json", params);
	}
	
	public void setNotifiers(UserInfo info) throws IOException, IllegalArgumentException {
		MiscUtil.validateNotEmpty("info", info);
		Map<String, String> params = new HashMap<String, String>();
		params.put("pin", pin);
		// TODO: setup other stuff
	}
	
	public String sendSMS(String number, String text, String callbackUrl) throws IOException {
		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("pin", pin);
			params.put("number", number);
			params.put("text", text);
			if (callbackUrl != null)
				params.put("callbackUrl", callbackUrl);
			String res = MiscUtil.doPost(GVMAX_BASE_API_URL+"/send.json", params);
			JSONObject jobj = new JSONObject(res);
			return jobj.getString("id");
		} catch (JSONException e) {
			throw new IOException(e);
		}
	}
	
	public static void main(String[] args) throws Exception {
		GVMaxAPI api = new GVMaxAPI("23c2ac842b893962fbf58b004b8344ec");
		UserInfo user = api.getUserInfo();
		System.out.println(user.getEmail()+" "+user.getSmsInCount());
	}
	
}
